import axios from "axios";
import store from "@/store";
import router from "@/router";
import {ElMessage} from 'element-plus'

axios.defaults.baseURL = "http://localhost:8081"
axios.interceptors.request.use(config => {
    return config
})

axios.interceptors.response.use(response => {
        let res = response.data;

        console.log("=================")
        console.log(res)
        console.log("=================")

        if (res.code === '200') {
            ElMessage.success({
                message: res.msg,
                type: 'success'
            })
            return response
        } if (res.code === '400') {
            ElMessage.warning({
                message: res.msg,
                type: 'warning'
            });
        } else {

            return Promise.reject(response.data.msg)
        }
    },
    error => {
        console.log(error)
        if(error.response.data) {
            error.message = error.response.data.msg
        }

        if(error.response.status === '401') {
            store.commit("removeUserInfo")
            router.push("/login")
        }

        return Promise.reject(error)
    }
)
